package com.nbicc.newsattachments.service.impl;

import com.nbicc.attachments.po.Attachment;
import com.nbicc.attachments.service.IAttachmentService;
import com.nbicc.common.RequestPageObj;
import com.nbicc.common.ResponseObj;
import com.nbicc.common.utils.ExceptionUtil;
import com.nbicc.newsattachments.mapper.NewsAttachmentsMapper;
import com.nbicc.newsattachments.po.NewsAttachments;
import com.nbicc.newsattachments.service.INewsAttachmentsService;
import com.nbicc.newscat.po.NewsCat;
import com.nbicc.parent.serivce.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lilingyan
 */
@Slf4j
@Service("NewsAttachmentsServieImpl")
public class NewsAttachmentsServieImpl extends BaseService implements INewsAttachmentsService{

    @Autowired
    private NewsAttachmentsMapper newsAttachmentsMapper;
    @Resource(name = "AttachmentServiceImpl")
    private IAttachmentService attachmentService;

    @Override
    public ResponseObj find(RequestPageObj<Map<String, Object>> requestPageObj) {
        List<Attachment> attachments = newsAttachmentsMapper.listAttachmentsByNewsId(requestPageObj.getData());
        return ResponseObj.success().setResult(attachments);
    }

    @Override
    public void insertAll(List<NewsAttachments> newsAttachmentsList) {
        if(newsAttachmentsList != null && newsAttachmentsList.size()>0){
            newsAttachmentsMapper.insertAll(newsAttachmentsList);
        }
    }

    /**
     *
     * @param newsId
     * @param newAttachments
     */
    public void incrementalModification(Long newsId,final List<Attachment> newAttachments){
        List<Attachment> oldAttachments = selectAttachmentsByNewsId(newsId);

        //遍历出原本有的 现在上传没有的附件
        oldAttachments.stream().filter(oldAttachment -> {
            if(newAttachments != null){
                for (Attachment newAttachment : newAttachments) {
                    if(oldAttachment.equals(newAttachment)){
                        return false;
                    }
                }
            }
            return true;
        }).forEach(oldAttachment -> {
            //删除
            try {
                attachmentService.deleteFile(oldAttachment.getId());
            } catch (IOException e) {
                e.printStackTrace();
                log.error(ExceptionUtil.getStackTraceInfo(e));
            }
        });

        //遍历出现在有的  原本没有的
        List<Attachment> incrementalAttachments = null;
        if(newAttachments != null){
            incrementalAttachments = newAttachments.stream().filter(newAttachment -> {
                for (Attachment oldAttachment : oldAttachments) {
                    if (oldAttachment.equals(newAttachment)) {
                        return false;
                    }
                }
                return true;
            }).collect(Collectors.toList());
        }

        //转化成关系 添加(文件已上传)
        if(incrementalAttachments != null && incrementalAttachments.size() > 0){
            List<NewsAttachments> newsAttachmentsList = new ArrayList<>();
            incrementalAttachments.stream().forEach(attachment->{
                NewsAttachments na = new NewsAttachments();
                na.setNewsId(newsId);
                na.setAttachmentId(attachment.getId());
                newsAttachmentsList.add(na);
            });
            newsAttachmentsMapper.insertAll(newsAttachmentsList);
        }

    }


    @Override
    public Integer deleteByMap(Map<String, Object> parms) {
        if(parms == null){
            return 0;
        }
        if(parms.get(NewsAttachments.FIELD_NEWS_ID) == null
                && parms.get(NewsAttachments.FIELD_ATTACHMENT_ID) == null ){     //防止删表
            return 0;
        }
        return  newsAttachmentsMapper.deleteByMap(parms);
    }

    @Override
    public void deleteByNewsId(Number id) throws IOException {
        List<Attachment> attachments = selectAttachmentsByNewsId(id);
        for (Attachment attachment : attachments) {
            attachmentService.deleteFile(attachment.getId());
        }
    }

    public List<Attachment> selectAttachmentsByNewsId(Number id) {
        Map<String, Object> params = new HashMap<>();
        params.put(NewsAttachments.FIELD_NEWS_ID,id);
        List<Attachment> attachments = newsAttachmentsMapper.listAttachmentsByNewsId(params);
        return attachments;
    }

}
